/**
 * @author Huy HCQ
 */

package pit.com.ordermobilesystem.http;

import java.lang.reflect.Type;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;




public  class AbstractParser<T extends HttpModel>  implements Parser<T>{
	Type typeClass;	
    /** 
     * All derived parsers must implement parsing a JSONObject instance of themselves. 
     */
    public T parse(JSONObject json) throws JSONException {
        Gson gson = new Gson();
        Type type = new TypeToken<T>(){}.getType();
        if(typeClass!=null) type=typeClass;
        return gson.fromJson(json.toString(), type); 
      }
    
    /**
     * Only the GroupParser needs to implement this.
     */
    public Group<T> parse(JSONArray array) throws JSONException {
        Gson gson = new Gson();
        Type type = new TypeToken<Group<T>>(){}.getType();   
        if(typeClass!=null) type=typeClass;
        return gson.fromJson(array.toString(), type);     
      }
    
    public T parse(String content){
    	return null;
    }

    public AbstractParser(Type typeClass) {
		super();
		this.typeClass = typeClass;
    }

    public AbstractParser() {
		super();
    }
    
    
}